package com.glory.gloryquickui.view

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.glory.gloryquickui.R
import com.glory.gloryquickui.activity.DemoActivity
import com.glory.gloryquickui.adapter.TablePortraitAdapter
import com.glory.gloryquickui.adapter.TablePortraitButtonAdapter
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.entity.TablePortraitBaseCustomBean
import com.glory.gloryquickui.entity.TablePortraitCustomBean
import com.glory.gloryquickui.viewmodel.QuickUIViewModel
import org.json.JSONArray

/**
 *Created:YJ
 *创建时间：2021/5/8
 *Describe:
 */
class TablePortraitViewController(context: Context?,val viewModel: QuickUIViewModel) : ViewController<FieldBean?>(context),
    TablePortraitButtonAdapter.TablePortraitButtonClickListener {

    private var rvTable: RecyclerView? = null
    private var mAdapter: TablePortraitAdapter? = null
    private var allList:ArrayList<TablePortraitBaseCustomBean> = arrayListOf()

    override fun resLayoutId(): Int {
        return R.layout.layout_table_portrait_view
    }

    override fun onCreatedView(view: View?) {
        rvTable = view?.findViewById(R.id.rvTable)
        val mLayoutManager = LinearLayoutManager(context)
        rvTable!!.layoutManager = mLayoutManager
    }

    private fun setAdapter(data: FieldBean?) {
//        val list = mutableListOf<TablePortraitCustomBean>()
//        data!!.tablePortraitData?.forEach {
//            val head = TablePortraitCustomBean(true)
//            list.add(head)
//            it.forEach { bean ->
//                val entity = TablePortraitCustomBean(false)
//                entity.textName = bean.textName
//                entity.textValue = bean.textValue
//                list.add(entity)
//            }
//        }
        allList = data?.tablePortraitData!!
        mAdapter = TablePortraitAdapter(allList,data?.operat?.children,this)
        rvTable!!.adapter = mAdapter
    }

    fun update(json: String?){
//        val list = mutableListOf<TablePortraitCustomBean>()
//        val fileBeanJsArray = JSONArray(json)
//        for (i in 0 until fileBeanJsArray.length()) {
//            val head = TablePortraitCustomBean(true)
//            list.add(head)
//            data?.config?.children?.forEach {
//                var jsonObject = fileBeanJsArray.getJSONObject(i)
//                val entity = TablePortraitCustomBean(false)
//                entity.textName = it.prop
//                if (jsonObject.has(it.prop)){
//                    entity.textValue = jsonObject.getString(it.prop)
//                }else{
//                    entity.textValue =""
//                }
//                list.add(entity)
//            }
//        }
//        mAdapter?.setNewData(list)
        //val list = arrayListOf<TablePortraitBaseCustomBean>()
        allList.clear()
        val fileBeanJsArray = JSONArray(json)
        for (i in 0 until fileBeanJsArray.length()) {
            val baseBean = TablePortraitBaseCustomBean()
            val childList = arrayListOf<TablePortraitCustomBean>()
            var jsonObject = fileBeanJsArray.getJSONObject(i)
            data?.config?.children?.forEach {
                val entity = TablePortraitCustomBean(false)
                entity.textName = it.label
                entity.textProp = it.prop
                if (jsonObject.has(it.prop)){
                    entity.textValue = jsonObject.getString(it.prop)
                }else{
                    entity.textValue =""
                }
                childList.add(entity)
            }
            if(data?.config?.dataType == "third" || data?.config?.dataType == "dataTable") { //数据源为数据表或数据包时取ID值，用于删除
                baseBean.ID = jsonObject.getString("ID")
            }
            baseBean.tablelineData = childList
            allList.add(baseBean)
        }
        mAdapter?.data = allList
        mAdapter?.notifyDataSetChanged()
    }

    fun removeData(position: Int){
        val jsonString = viewModel.pageData.value?.get(data!!.vModel)
        val fileBeanJsArray = JSONArray(jsonString)
        fileBeanJsArray.remove(position)
        val map = viewModel.pageData.value!!.toMutableMap()
        map[data!!.vModel] = fileBeanJsArray.toString()
        viewModel.pageData.value = map
    }

    override fun onBindView(data: FieldBean?) {
        setAdapter(data)
    }

    override fun tabButtonClick(position: Int, view: View, field: FieldBean) {
        setEvent(field,position)
    }

    private fun setEvent(btnBean: FieldBean,position: Int) {
        when (btnBean.config.eventType) {
            "manual" -> {
                if (btnBean.config.eventName == "saveForm") {
                    viewModel.saveData()
                } else if (btnBean.config.eventName == "clearForm") {
                    viewModel.searchData()
                } else if (btnBean.config.eventName == "backPage") {

                } else if (btnBean.config.eventName.contains("get")) {
                    viewModel.searchData()
                } else if (btnBean.config.eventName.contains("delete")) {
                    if(data?.config?.dataType == "third") {
                        //数据包
                        viewModel.deleteThirdData(data!!.vModel,allList.get(position).ID,position)
                    }else{
                        viewModel.deleteData(data!!.vModel,allList.get(position).ID,position)
                    }

                }
            }
            "interface" -> {

            }
            "froms" -> {
                val map = HashMap<String, String>()
                allList.get(position).tablelineData?.forEach {
                    map[it.textProp] = it.textValue
                }
                val intent = Intent(context, DemoActivity::class.java)
                intent.putExtra("json",btnBean.config.fromId)
                intent.putExtra("map",map)
                context!!.startActivity(intent)
            }
            "pageInteraction" -> {

            }
        }
    }
}